using GameStateManagement.View.ScreenManager;
using GameStateManagement.View.Screens;
using Microsoft.Xna.Framework;
using System;

namespace GameStateManagement
{
    public class GameStateManagementGame : Game
    {
        #region Fields

        readonly GraphicsDeviceManager _graphics;
        readonly ScreenManager _screenManager;

        #endregion

        #region Initialization

        public GameStateManagementGame()
        {
            Content.RootDirectory = "Content";

            _graphics = new GraphicsDeviceManager(this)
                           {
                               IsFullScreen = true
                           };
            TargetElapsedTime = TimeSpan.FromTicks(333333);


            InitializeLandscapeGraphics();

            _screenManager = new ScreenManager(this);

            Components.Add(_screenManager);

            // attempt to deserialize the screen manager from disk. if that
            // fails, we add our default screens.
            if (!_screenManager.DeserializeState())
            {
                // Activate the first screens.
                _screenManager.AddScreen(new BackgroundScreen(), null);
                _screenManager.AddScreen(new MainMenuScreen(), null);
            }
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            // serialize the screen manager whenever the game exits
            _screenManager.SerializeState();

            base.OnExiting(sender, args);
        }

        private void InitializePortraitGraphics()
        {
            _graphics.PreferredBackBufferWidth = 480;
            _graphics.PreferredBackBufferHeight = 800;
        }

        private void InitializeLandscapeGraphics()
        {
            _graphics.PreferredBackBufferWidth = 800;
            _graphics.PreferredBackBufferHeight = 480;
        }

        #endregion

        #region Draw

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            _graphics.GraphicsDevice.Clear(Color.Black);

            // The real drawing happens inside the screen manager component.
            base.Draw(gameTime);
        }

        #endregion
    }

    #region Entry Point

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (var game = new GameStateManagementGame())
            {
                game.Run();
            }
        }
    }

    #endregion
}
